// pages/means/means.js
let md5 = require("../../utils/md5.js")
let axios = require("../../utils/util.js")
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nickname: "",
    autograph: "",
    sexList: ["男", "女"],
    sexindex: 0,
    birthday: "2021-5-10",
    headimg: "",
    headimg_: '',
  },
  nicknameIpt(data) {
     //获取到的用户昵称        
     let nickname = data.detail.value
     //检测是否含有表情
     var regRule = /\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g;
     if (nickname.match(regRule)) {
      nickname = nickname.replace(/\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g, "");
     }
    this.setData({
      nickname: nickname
    })
  },
  autographIpt(data) {
    let autograph = data.detail.value
    //检测是否含有表情
    var regRule = /\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g;
    if (autograph.match(regRule)) {
      autograph = autograph.replace(/\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g, "");
    }
    this.setData({
      autograph: autograph
    })
  },
  sexChange(data) {
    console.log('picker发送选择改变，携带值为', data.detail.value, parseInt(data.detail.value))
    this.setData({
      sexindex: data.detail.value
    })
  },
  birthdayChange(data) {
    console.log('picker发送选择改变，携带值为', data.detail.value)
    this.setData({
      birthday: data.detail.value
    })
  },
  // Oncarm: function(e) {
  //   var whopic = e.currentTarget.dataset.whopic;
  //   /**
  //    * page_that:调用方法的页面，
  //    * cb_key:返回参数数组的名称,
  //    * count:上传张数,
  //    * 返回值：数组
  //    */
  //   axios.selecImg(this, whopic, 1);
  // },
  chooseImg() {
    var _this = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        var tempFilePaths = res.tempFilePaths
        // console.log(tempFilePaths)
        _this.setData({
          headimg: tempFilePaths[0]
        })
        let isgif = _this.isgif(tempFilePaths[0])
        console.log(isgif);
        if (isgif) {
          _this.setData({
            headimg: _this.data.headimg_,
          })
          wx.showToast({
            title: '不支持gif格式,请重新选择',
            icon: "none",
            duration: 5000 //持续的时间
          })
        }
      }
    })
  },
  isgif(value) {
    if (value) {
      let type = [];
      type = value.split(".").pop();
      if (type == 'gif') {
        return true;
      } else {
        return false;
      }
    }
  },
  async meansSub() {
    let _this = this;
    let timestamp = (Date.parse(new Date()) / 1000).toString();
    let data = {
      type: '1',
      userId: app.globalData.user_id,
      token: app.globalData.userToken,
      sex: parseInt(_this.data.sexindex) + 1,
      originSign: _this.data.autograph,
      birthday: _this.data.birthday,
      nickName: _this.data.nickname,
      time: timestamp,
    }
    let _token = axios.encryption(data);
    let sign = md5.hexMD5(_token);
    await wx.uploadFile({
      url: axios.testInterface + '/user/editUserProfile', //完善个人资料
      filePath: _this.data.headimg,
      name: 'headPic',
      formData: {
        type: '1',
        userId: app.globalData.user_id,
        token: app.globalData.userToken,
        sex: parseInt(_this.data.sexindex) + 1,
        originSign: _this.data.autograph,
        birthday: _this.data.birthday,
        nickName: _this.data.nickname,
        time: timestamp,
        sign: sign,
      },
      success(res) {
        var data = res.data
        const strToObj = JSON.parse(data)
        console.log("111", strToObj);
        if (strToObj.status == 0) {
          _this.setData({
            headimg: strToObj.result.url
          })
        } else {
          wx.showToast({
            title: strToObj.text,
            icon: "none"
          })
        }

      }
    })
    let params = {
      userId: app.globalData.user_id,
      token: app.globalData.userToken,
      sex: parseInt(_this.data.sexindex) + 1,
      originSign: _this.data.autograph,
      birthday: _this.data.birthday,
      nickName: _this.data.nickname,
      headPic: _this.data.headimg,
    };
    axios.axios({
      url: "/user/editUserProfile",
      data: params
    }).then((res) => {
      if (res.data.status == 0) {
        wx.showToast({
          title: res.data.text,
          icon: "none"
        })
        _this.user_info()
        setTimeout(() => {
          wx.navigateBack({
            delta: 1,
          })
        }, 1500)
      } else {
        wx.showToast({
          title: res.data.text,
          icon: "none"
        })
      }
    })
  },
  tomyalbum() {
    wx.navigateTo({
      url: '/pages/myalbum/myalbum',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.user_info()
  },
  user_info() {
    let params = {
      userId: app.globalData.user_id,
      token: app.globalData.userToken,
    };
    axios.axios({
      url: "/user/userInfo",
      data: params
    }).then((res) => {
      // console.log(res);
      this.setData({
        headimg: res.data.result.user.headPic,
        headimg_: res.data.result.user.headPic,
        birthday: res.data.result.user.birthday,
        nickname: res.data.result.user.nickname,
        sexindex: res.data.result.user.sex - 1,
        autograph: res.data.result.user.originSign,
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
